<?php
/* @var $this SearchController */

$this->pageTitle=Yii::app()->name;

?>

<div class="large-12 columns">
	<h1>Search in catalogs</h1>
</div>

<form method="GET" action="<?php echo $this->createUrl('index') ?>">
	<div class="small-8 columns small-centered">

		<div class="row collapse ">
			<div class="small-10 columns">
				<input type="search" name="q" placeholder="Search in catalogs..." value="<?php echo $q; ?>" />
			</div>
			<div class="small-2 columns">
				<input type="submit" class="postfix small button" value="Search" />
			</div>
		</div>

	</div>
</form>

<hr class="large-9 columns small-centered" />

<ul class="large-9 columns no-bullet large-centered">
	<?php
		if($isSearch){
			echo count($rows).' results found.';
		}
		if (count($rows) > 0)
		{
			$q = preg_replace("/  +/"," ",trim($q));
			$allKeywords = explode(' ', $q);
			$q1 = str_replace(" ", "|", preg_replace("/  +/"," ",trim($q)));
			foreach ($rows as $row) {

				$allStatements = explode('.', $row['content']);

				$result = array();
				foreach ($allStatements as $statement) {
					foreach ($allKeywords as $keyWord) {
						if (substr_count($statement, $keyWord)){
							$result[] = $statement.' ... ';
							break;
						}
					}
				}
				if($row['foreign_type'] == SearchIndexType::Catalog){
					$url = Yii::app()->createUrl('catalog/view', array('id'=>$row['foreign_id']));
				}
				if($row['foreign_type'] == SearchIndexType::CatalogItem){
					$url = Yii::app()->createUrl('catalogItem/view',array('id'=>$row['foreign_id']));
				}
				if($row['foreign_type'] == SearchIndexType::CatalogItem){
					$url = Yii::app()->createUrl('news/view',array('id'=>$row['foreign_id']));
				}

				echo '<li class="row">
						<div class="small-2 columns"><img src="http://placehold.it/80x80&amp;text=[img]" /></div>
						<div class="small-10 columns">
							<a href="'.$url.'">'.preg_replace("/".$q1."/i", '<b>\\0</b>', $row['title']).'</a>
							<div>'.preg_replace("/".$q1."/i", '<b>\\0</b>', implode(' ... ',$result)).'
							</div>
						</div>
					</li>';
			}
		}
	?>

</ul>